from django.shortcuts import render

# Create your views here.
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView

from goods import constants
from goods.models import SKU
from goods.search_indexes import SKUIndexSerializer
from goods.serializers import HotGoodsSerializer

'''热销商品'''
class HotGoods(ListAPIView):
    pagination_class=None
    serializer_class = HotGoodsSerializer

    def get_queryset(self):
        category_id=self.kwargs['category_id']
        # print(category_id)
        return SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:3]
        # return SKU.objects.all()



class GoodsDetialList(ListAPIView):
    serializer_class=HotGoodsSerializer

    filter_backends = (OrderingFilter,)
    ordering_fields = ('create_time', 'price', 'sales')

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id,is_launched=True)



from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer